package com.ittsang.service.impl;

import com.alibaba.excel.EasyExcel;
import com.ittsang.dto.R;
import com.ittsang.dto.UserDTO;
import com.ittsang.mapper.UserClubPostsMapper;
import com.ittsang.mapper.UserMapper;
import com.ittsang.mapper.UserRaceMapper;
import com.ittsang.pojo.User;
import com.ittsang.pojo.UserClubPosts;
import com.ittsang.service.ExcelService;
import com.ittsang.utils.ExcelUtils;
import com.ittsang.utils.UserHolder;
import com.ittsang.vo.UserVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class ExcelServiceImpl implements ExcelService {

    @Resource
    private UserRaceMapper userRaceMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserClubPostsMapper userClubPostsMapper;

    @Override
    public void raceExcel(Integer raceId, HttpServletResponse response) {

        UserDTO user = UserHolder.getUser();
        UserClubPosts userClubPosts = userClubPostsMapper.getUserRole(Math.toIntExact(user.getId()), user.getClubId());
        if (userClubPosts == null) {
            response.setStatus(401);
            return;
        }

        List<Integer> i = userRaceMapper.getUserList(raceId);
        List<UserVO> userList = userMapper.getExcelUser(i);
        response.setContentType("application/vnd.ms-excel");  // excel 的响应头
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+"test.xlsx");//或者文件名后缀为xlsx
        try {
            EasyExcel.write(response.getOutputStream(), UserVO.class).sheet("模板").doWrite(userList);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
